﻿using System;
using System.Linq;
using System.Windows.Forms;
using BlueDot.Mobile.Core;
using Action=BlueDot.Mobile.Core.Action;

namespace TrackPreviousView.Views
{
    public class DialogViewBase : ViewBase
    {
        #region Fields

        private MenuItem backMenuItem;
        private MainMenu mainMenu;

        #endregion

        #region Constructors

        public DialogViewBase()
        {
            InitializeComponent();
        }

        [DependencyConstructor]
        public DialogViewBase(IMobileApplication application)
            : base(application)
        {
            InitializeComponent();

            AddViewShowing(application);
        }

        #endregion

        #region Properties

        protected IView PreviousView { get; set; }

        #endregion

        #region public

        public bool OnBack()
        {
            WorkFlowUtils.ShowView(this, Application, PreviousView);
            return true;
        }

        public void OnViewShowing(object sender, EventArgs e)
        {
            // Replace the main menu on the shell with our view's menu
            ((Shell) Application.Shell).Menu = mainMenu;

            PreviousView = Application.Shell.ActiveView;
        }

        #endregion

        #region private

        private void AddViewShowing(IMobileApplication application)
        {
            string actionAddress = ViewShowingKey();

            if (!string.IsNullOrEmpty(actionAddress))
            {
                Action targetAction =
                    (application.Actions.Where(action => action.Address == actionAddress)).SingleOrDefault();

                if (targetAction == null)
                {
                    targetAction = new Action(actionAddress);
                    targetAction.Application = application;
                    application.Actions.Add(targetAction);
                }

                targetAction.RegisterReceiver(this, "OnViewShowing");
            }
        }

        private void InitializeComponent()
        {
            mainMenu = new MainMenu();
            backMenuItem = new MenuItem();
            SuspendLayout();
            // 
            // mainMenu
            // 
            mainMenu.MenuItems.Add(backMenuItem);
            // 
            // backMenuItem
            // 
            backMenuItem.Text = "Back";
            // 
            // DialogViewBase
            //             
            Name = "DialogViewBase";
            // 
            // Actions
            // 
            ActionSenders.Add(new MetaActionSender(backMenuItem, "Click", this, "OnBack",
                                                   "action://DialogViewBase/backMenuItem/Click"));
            RegisterActionSenders();
            ResumeLayout(false);
        }

        #endregion

        #region protected

        protected virtual string ViewShowingKey()
        {
            return string.Empty;
        }

        #endregion
    }
}